package com.jixiantubu.luntan;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.jixiantubu.adapter.TieziShouyeAdapter;
import com.jixiantubu.bean.PostInfoBean;
import com.jixiantubu.jinnang.JinNang_YouJiActivity;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class QuanbuFragment extends Fragment {
	@ViewInject(R.id.post_listview)
	PullToRefreshListView listView;
	HttpUtils httpUtils;
	View view;
	TieziShouyeAdapter adapter;
	Context context;
	BitmapUtils bitmapUtils;
	List<PostInfoBean> list = new ArrayList<PostInfoBean>();
	String url = "http://" + MyApplication.getIP() + ":8080/Jinnang/post_servlet";
	private int number = 1;// 默认第一页
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view=inflater.inflate(R.layout.activity_tiezi_,null);
		ViewUtils.inject(view);
		bitmapUtils = new BitmapUtils(getActivity());
		initViews();
		setListView();
		initData(url);
		return view;
	}
	private void initViews() {
		listView = (PullToRefreshListView)view.findViewById(R.id.post_listview);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),
						Tiezi_Info_Activity.class);
				intent.putExtra("position",list.get(position));
				startActivity(intent);
			}
		});
		
	}
	private void initData(String url) {
		adapter = new TieziShouyeAdapter(getActivity(), list);
		listView.setAdapter(adapter);
		httpUtils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("key", "1");
		params.addQueryStringParameter("page", "1");
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						String result = responseInfo.result;		 
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<PostInfoBean>>() {
						}.getType();
						List<PostInfoBean> list2  = gson.fromJson(result, typeOfT);	
						list.addAll(list2);
						adapter.setList(list);
						adapter.notifyDataSetChanged();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});
	}
	private void setListView() {
		// 设置PullToRefreshListView添加上拉和下拉的方式
		listView.setMode(Mode.BOTH);// 现在是两种都用的情况

		// 在布局文件中通过添加属性(ptr:ptrAnimationStyle="rotate")控制加载图标的动画效果(旋转[rotate]
		// 翻转[flip])

		// 修改刷新布局的参数
		/*
		 * includeStart = true, includeEnd = false;表示获取下拉刷新的布局
		 * 
		 * includeStart = false, includeEnd = true;表示获取上拉刷新的布局
		 */
		ILoadingLayout freshLayout = listView
				.getLoadingLayoutProxy(true, false);

		// 修改更新的时间，可以用于提示上次刷新是什么时候
		freshLayout.setLastUpdatedLabel("上次更新2015.10.20");
		// 修改更新的旋转图标
		// freshLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.ic_launcher));
		// 设置下拉的文字
		freshLayout.setPullLabel("下拉刷新");
		// 松开的提示语
		freshLayout.setReleaseLabel("松开刷新");
		// 设置刷新的文字提示
		freshLayout.setRefreshingLabel("正在刷新!请稍等");
		// 设置字体格式
		// freshLayout.setTextTypeface(Typeface.DEFAULT);

		/**
		 * 给PullToRefreshListView添加headView
		 */
	/*	ListView list1 = listView.getRefreshableView();// 获取PullToRefreshListView中的ListView
		ImageView img = new ImageView(this);
		// img.setImageResource(R.drawable.wo);
		list1.addHeaderView(img);
*/
		/**
		 * 为PullToRefreshListView绑定监听器监听用户的下拉和上拉操作
		 */
		listView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			// 下拉刷新
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// 下拉刷新的逻辑代码
				/*
				 * 1、清空数据集合中的原有的所有的数据
				 * 
				 * 2、重新请求第一页数据，存入到数据集合中
				 * 
				 * 3、通知UI展示新数据(notifyDataSetChanged)
				 * 
				 * 4、通知刷新布局，数据加载完成
				 */

				// 1、清空数据集合中的原有的所有的数据
				list.clear();
				// 2、重新请求第一页数据，存入到数据集合中 3、通知UI展示新数据(notifyDataSetChanged)
				initData(url);
				// 4、通知刷新布局，数据加载完成
				listView.onRefreshComplete();
			}

			// int number = (Integer)(adapter.getCount()/5) ;
			protected void addMoreData() {
				// 刷新之后显示下一页内容，当亲为第一页
				number++;// 页数自加；
				if (0 != adapter.getCount()%5) {
					Toast.makeText(getActivity(), "已全部显示",
							Toast.LENGTH_SHORT).show();
				} else {
					String url = "http://" + MyApplication.getIP() + ":8080/Jinnang/post_servlet?key=1&page="
							+ number;
					initData(url);

				}
			}

			@Override
			// 上拉加载
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				// 上拉加载的逻辑代码
				/*
				 * 1、请求下一页数据
				 * 
				 * 2、添加到数据集合中
				 * 
				 * 3、通知UI展示新数据(notifyDataSetChanged)
				 * 
				 * 4、通知刷新布局，数据加载完成
				 */
				addMoreData();
				/*
				 * // 1、请求下一页数据 page++; // 2、添加到数据集合中
				 * 3、通知UI展示新数据(notifyDataSetChanged) initData(page);
				 */
				// 4、通知刷新布局，数据加载完成
				listView.onRefreshComplete();

			}
		});

	}
}
